package com.lw.server;

import com.lw.common.Message;
import com.lw.common.MessageType;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;

/**
 * 和客户端保持通信的线程
 */
@AllArgsConstructor
public class ServerConnectClientThread extends Thread{

    @Getter
    private Socket socket;
    private String userId;

    @Override
    public void run() {
        while (true){
            try {
                ObjectInputStream inputStream = new ObjectInputStream(socket.getInputStream());
                Object obj = inputStream.readObject();
                if(obj instanceof Message msg){
                    if(msg.getMsgType().equals(MessageType.MESSAGE_GET_ONLINE_FRIEND)){
                        System.out.println(msg.getSender()+" 请求在线用户列表");
                        String onlineUsersStr = ManageClientThread.getOnlineUser();
                        //构建Message对象返回在线用户列表信息
                        Message retMsg = new Message();
                        retMsg.setMsgType(MessageType.MESSAGE_RET_ONLINE_FRIEND);
                        retMsg.setContent(onlineUsersStr);
                        retMsg.setReceiver(msg.getSender());

                        ObjectOutputStream outputStream = new ObjectOutputStream(socket.getOutputStream());
                        outputStream.writeObject(retMsg);
                    } else if (msg.getMsgType().equals(MessageType.MESSAGE_CLIENT_EXIT)) {//客户端退出
                        String senderId = msg.getSender();
                        System.out.println(senderId+" 退出了系统");
                        ManageClientThread.removeClientThread(senderId);//从线程池中移除线程
                        socket.close();//关闭socket
                        break;//退出循环
                    }else if (msg.getMsgType().equals(MessageType.MESSAGE_COMM_MES)) {//私聊
                        String senderId = msg.getSender();
                        String receiveId = msg.getReceiver();
                        System.out.println(senderId+" 发送消息给 "+receiveId+"["+msg.getSendTime()+"]");

                        //此处的ServerConnectClientThread的线程是senderId的线程，也是senderId的socket，所以需要获取receiveId的线程和socket
                        Socket reveiveSocket = ManageClientThread.getClientThread(receiveId).getSocket();
                        ObjectOutputStream outputStream = new ObjectOutputStream(reveiveSocket.getOutputStream());
                        outputStream.writeObject(msg);//转发消息
                    }else if (msg.getMsgType().equals(MessageType.MESSAGE_TO_ALL_MES)) {//群聊
                        String senderId = msg.getSender();
                        String receiveIds = msg.getReceiver();
                        System.out.println(senderId+" 发送群聊消息 "+"["+msg.getSendTime()+"]");

                        String[] receiveIdArr = receiveIds.split(",");
                        for (String receiveId : receiveIdArr) {
                            Socket reveiveSocket = ManageClientThread.getClientThread(receiveId).getSocket();
                            ObjectOutputStream outputStream = new ObjectOutputStream(reveiveSocket.getOutputStream());
                            outputStream.writeObject(msg);//转发消息
                        }
                    }else if (msg.getMsgType().equals(MessageType.MESSAGE_FILE_MES)) {//文件发送
                        String senderId = msg.getSender();
                        String receiveId = msg.getReceiver();
                        System.out.println(senderId+" 发送文件给 "+receiveId+"["+msg.getSendTime()+"]");

                        Socket reveiveSocket = ManageClientThread.getClientThread(receiveId).getSocket();
                        ObjectOutputStream outputStream = new ObjectOutputStream(reveiveSocket.getOutputStream());
                        outputStream.writeObject(msg);//转发消息
                    }else if (msg.getMsgType().equals(MessageType.MESSAGE_BIG_FILE_MES)) {//大文件发送
                        String senderId = msg.getSender();
                        String receiveId = msg.getReceiver();
                        System.out.println(senderId+" 发送文件给 "+receiveId+"["+msg.getSendTime()+"]");

                        Socket reveiveSocket = ManageClientThread.getClientThread(receiveId).getSocket();
                        ObjectOutputStream outputStream = new ObjectOutputStream(reveiveSocket.getOutputStream());
                        outputStream.writeObject(msg);//转发消息
                    }
                }
            } catch (IOException e) {
                throw new RuntimeException(e);
            } catch (ClassNotFoundException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
